import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: () => import('@/views/login')
  },
  {
    path: '/',
    redirect:"/welcome",
    component: () => import('@/views/home/home'),
    children:[
      {
        path:'welcome',
        component:()=>import('@/views/Welcome.vue')
      },
      {
        path:'users',
        component:()=>import('@/views/Users/Users')
      },
      {
        path: 'rights',
        component: () => import('@/views/power/Right')
      },
      {
        path: 'roles',
        component: () => import('@/views/power/Roles')
      },
      {
        path: 'categories',
        component: () => import('@/views/goods/Cate')
      },
      {
        path: 'params',
        component: () => import('@/views/params/Params')
      },
      {
        path: 'goods',
        component: () => import('@/views/goods/GoodList')
      },
      {
        path: 'add',
        component: () => import('@/views/goods/AddGood')
      },
      {
        path: 'orders',
        component: () => import('@/views/order/order')
      },
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    return next()
  }
  const token = sessionStorage.getItem('TOKEN')
  if (!token) {
    return next('/login')
  }
  next()
})

export default router
